package com.suneast.suneaststore.modules.search;

import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suneast.suneaststore.API;
import com.suneast.suneaststore.bean.search.SearchFilterItemBean;
import com.suneast.suneaststore.modules.search.event.FilterGotEvent;
import com.suneast.suneaststore.modules.search.event.SearchResultEvent;
import com.suneast.suneaststore.util.net.RestClient;
import com.suneast.suneaststore.bean.search.SearchBean;
import com.suneast.suneaststore.bean.search.SearchFilterBean;
import com.suneast.suneaststore.modules.search.event.SearchPageEvent;
import com.suneast.suneaststore.util.log.LatteLogger;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.WeakHashMap;


public class SearchRequestHandler {
    public static void requestData(WeakHashMap<String, Object> paramsMap) {
        RestClient.builder()
                .url(API.GOODS_LIST)
                .params(paramsMap)
                .success((String response) -> {
                    JSONObject rootJObj = JSONObject.parseObject(response);
                    LatteLogger.json("test", response);
                    if (rootJObj.getIntValue("code") == 1) {
                        JSONObject dataJObj = rootJObj.getJSONObject("data");
                        if (dataJObj.getIntValue("count") > 0) {
                            JSONArray jsonList = dataJObj.getJSONArray("list");
                            if (jsonList != null) {
                                if (jsonList.size() > 0) {
                                    ArrayList<SearchBean> searchBeans = new ArrayList<>();
                                    for (int i = 0, length = jsonList.size(); i < length; i++) {
                                        SearchBean bean = new SearchBean();
                                        JSONObject jObj = jsonList.getJSONObject(i);
                                        bean.setId(jObj.getIntValue("id"));
                                        bean.setThumb(jObj.getString("thumb"));
                                        bean.setMrecommand(jObj.getIntValue("mrecommand"));
                                        bean.setTitle(jObj.getString("title"));
                                        bean.setMemberprice(jObj.getString("memberprice"));
                                        bean.setReal_price(jObj.getString("real_price"));
                                        bean.setDiscount_price(jObj.getString("discount_price"));
                                        bean.setUnit(jObj.getString("unit"));
                                        bean.setCoin_reward(jObj.getDouble("coin_reward"));
                                        searchBeans.add(bean);
                                    }
                                    EventBus.getDefault().post(new SearchResultEvent(searchBeans));
                                } else {
                                    EventBus.getDefault().post(new SearchResultEvent(true));
                                }
                            } else {
                                EventBus.getDefault().post(new SearchResultEvent(true));
                            }
                        } else {
                            EventBus.getDefault().post(new SearchResultEvent(true));
                        }
                    }
                })
                .build()
                .post();
    }

    public static void requestPage(final WeakHashMap<String, Object> paramsMap) {
        RestClient.builder()
                .url(API.GOODS_LIST)
                .params(paramsMap)
                .success((String response) -> {
                    LatteLogger.json("test", response);
                    JSONObject rootJObj = JSONObject.parseObject(response);
                    if (rootJObj.getIntValue("code") == 1) {
                        JSONObject dataJObj = rootJObj.getJSONObject("data");

                        if (dataJObj.getIntValue("count") > 0) {
                            JSONArray jsonList = dataJObj.getJSONArray("list");
                            if (jsonList != null) {
                                if (jsonList.size() > 0) {
                                    ArrayList<SearchBean> searchBeans = new ArrayList<>();
                                    for (int i = 0, length = jsonList.size(); i < length; i++) {
                                        SearchBean bean = new SearchBean();
                                        JSONObject jObj = jsonList.getJSONObject(i);
                                        bean.setId(jObj.getIntValue("id"));
                                        bean.setThumb(jObj.getString("thumb"));
                                        bean.setMrecommand(jObj.getIntValue("mrecommand"));
                                        bean.setTitle(jObj.getString("title"));
                                        bean.setMemberprice(jObj.getString("memberprice"));
                                        bean.setReal_price(jObj.getString("real_price"));
                                        bean.setDiscount_price(jObj.getString("discount_price"));
                                        bean.setUnit(jObj.getString("unit"));
                                        bean.setCoin_reward(jObj.getDouble("coin_reward"));
                                        searchBeans.add(bean);
                                    }
                                    int total = dataJObj.getIntValue("total");
                                    int count = dataJObj.getIntValue("count");
                                    Log.d("debug", "total = " + total + "count = " + count);
                                    SearchPageEvent event = new SearchPageEvent(searchBeans);
                                    event.setTotal(total);
                                    EventBus.getDefault().post(event);
                                } else {
                                    EventBus.getDefault().post(new SearchPageEvent(true));
                                }
                            } else {
                                EventBus.getDefault().post(new SearchPageEvent(true));
                            }
                        } else {
                            EventBus.getDefault().post(new SearchPageEvent(true));
                        }
                    }
                })
                .build()
                .post();
    }

    public static void requestFilters(int cate, String filters) {
        RestClient.builder()
                .url(API.GOODS_FILTER)
                .params("cate", cate)
                .params("filter", filters)
                .success((String response) -> {
                    LatteLogger.json("test", response);
                    JSONObject rootJObj = JSONObject.parseObject(response);
                    if (rootJObj.getIntValue("code") == 1) {
                        JSONArray dataJArray = rootJObj.getJSONArray("data");
                        if (dataJArray != null && dataJArray.size() > 0) {
                            ArrayList<SearchFilterBean> beanList = new ArrayList<>();
                            for (int i = 0, length = dataJArray.size(); i < length; i++) {
                                SearchFilterBean bean = new SearchFilterBean();
                                bean.setTitle(dataJArray.getJSONObject(i).getString("type_name"));
                                JSONArray jArray = dataJArray.getJSONObject(i).getJSONArray("list");
                                if (jArray != null && jArray.size() > 0) {
                                    for (int j = 0, jLength = jArray.size(); j < jLength; j++) {
                                        JSONObject jObj = jArray.getJSONObject(j);
                                        SearchFilterItemBean itemBean = new SearchFilterItemBean();
                                        itemBean.setKey(jObj.getIntValue("key"));
                                        itemBean.setValue(jObj.getIntValue("value"));
                                        Log.e("test", "requestFilters: name = " + jObj.getString("name"));
                                        itemBean.setName(jObj.getString("name"));
                                        itemBean.setCurrent(jObj.getBoolean("current"));
                                        bean.getItemList().add(itemBean);
                                    }
                                    bean.setShowTextAll(jArray.size() > 3);
                                }
                                beanList.add(bean);
                            }
                            EventBus.getDefault().post(new FilterGotEvent(beanList));
                        } else {
                            EventBus.getDefault().post(new FilterGotEvent(new ArrayList<>()));
                        }
                    }

                })
                .error((code, msg) -> {
                    LatteLogger.e("error", " requestFilters error code = " + code + " msg = " + msg);
                })
                .failure(() -> {
                    LatteLogger.e("error", " requestFilters failure ");
                })
                .build()
                .post();
    }

}
